package com.app.time;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class RegistActivity extends Activity {
	 private static  String url="http://10.0.2.2:8080/TimeWeb/regist.action?";
	 private final String url_constant="http://10.0.2.2:8080/TimeWeb/regist.action?";
	 private EditText uname;
	 private EditText upass;
	 private EditText uemail;
	 private Button btnRegist;
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
			 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()   
	         .detectDiskReads()   
	         .detectDiskWrites()   
	         .detectNetwork()   // or .detectAll() for all detectable problems   
	         .penaltyLog()   
	         .build());   
			  StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()   
			         .detectLeakedSqlLiteObjects()   
			         //.detectLeakedClosableObjects()   
			         .penaltyLog()   
			         .penaltyDeath()   
			         .build());
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.regist);
	        initView();
	        setListener();
	    }
	    
		private void initView() {
			btnRegist=(Button)findViewById(R.id.btnRegist);
			uname=(EditText)findViewById(R.id.uname);
			upass=(EditText)findViewById(R.id.upass);
			uemail = (EditText)findViewById(R.id.uemail);
		}
		private void setListener() {
			btnRegist.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					String uname = RegistActivity.this.uname.getText().toString();
					Log.v("uname = ", uname);
					String upass = RegistActivity.this.upass.getText().toString();
					Log.v("upass = ", upass);
					String uemail = RegistActivity.this.uemail.getText().toString();
					Log.v("uemail",uemail);
					registRemoteService(uname,upass,uemail);
				}
			});
		}
		
		
	    public void registRemoteService(String uname,String upass,String uemail){
	        String result=null;
	    	try {
		    	HttpClient httpclient = new DefaultHttpClient();
		    	url= url_constant+"uname="+uname+"&upass="+upass+"&uemail="+uemail;
		    	HttpGet request=new HttpGet(url);
		    	request.addHeader("Accept","text/json");
				HttpResponse response =httpclient.execute(request);
				HttpEntity entity = response.getEntity();
				String json = EntityUtils.toString(entity,"UTF-8");
				if(json != null){
					JSONObject jsonObject=new JSONObject(json);
					result=jsonObject.get("message").toString();
					if (result.equals("注册成功")) {
						Intent intent = new Intent(RegistActivity.this,Success.class);
						startActivity(intent);
					}else{
						Log.d("message","注册失败");
					}
				}
			   if(result == null){  
				   json="注册失败";
				  Log.d("message","网络不通");
			   }
	    	 } catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (JSONException e) { 
				e.printStackTrace();
			}
	    }
}
